<?php
namespace app\adminxcx\controller;
use \think\Controller;
use app\admin\model\Card as CardModel;
use app\admin\model\Job as JobModel;
use app\admin\model\Company as CompanyModel;
use app\admin\model\Info as InfoModel;
use app\admin\model\Group as GroupModel;
use EasyWeChat\Factory;

class Card extends Controller
{

    protected $CardModel;
    protected $JobModel;
    protected $CompanyModel;
    protected $InfoModel;
    protected $GroupModel;

    public function __construct()
    {
        if(!session('uid')){
            $this->redirect('index/index');
        }
        $this->CardModel = new CardModel();
        $this->JobModel = new JobModel();
        $this->CompanyModel = new CompanyModel();
        $this->InfoModel = new InfoModel();
        $this->GroupModel = new GroupModel();
    }

    public function edit($cid,$gid){
        $cardRes = $this->CardModel->get($cid);
        if(request()->isPost()){
            $data = input();
            $data['id'] = $cid;
            unset($data['cid']);
            $res =  $cardRes-> save($data);
            if($res){
                return 1;
            }else{
                return 0;
            }
        }

        $query = [
            'where' => [
                'gid' => $gid
            ],
            'order' => [
                'update_time' => 'desc'
            ]
        ];
        $jobRes = $this->JobModel->getLst($query);
        $companyRes = $this->CompanyModel->getLst($query);
        $uid = session('uid');
        return view('edit',[
            'card'=>$cardRes,
            'job'=> $jobRes,
            'company'=>$companyRes,
            'uid' => $uid,
            'token' => cache('token_admin'.$uid)
        ]);
    }

    public function cropper(){
        return view();
    }

    public function getCode($cid=0)
    {
        $cardRes = $this->CardModel -> get($cid);
        $infoRes = $this->InfoModel->where('gid',$cardRes->gid)->find();
        $adminLevel = session('adminLevel');
        if($adminLevel > 0){
            if($infoRes['wechatappid'] || $infoRes['wechatsecret']){
                $config = [
                    'app_id' => $infoRes['wechatappid'],
                    'secret' => $infoRes['wechatsecret'],
                ];
            }else{
                return '你尚未设置APPID与SECRET,请联系管理员'; 
            }
        }else{
            $config = [
                'app_id' => config('wechat.mini_program.default.app_id'),
                'secret' => config('wechat.mini_program.default.secret'),
            ];
        }
        $weChatApp = Factory::miniProgram($config);
        $path = '/pages/my/index?id='.$cid.'&type=1';
        $directoryCwd = getcwd();
        // dump($directoryCwd );die;
        $response = $weChatApp->app_code->get($path);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs($directoryCwd.'/codeimg', 'comma'.$cardRes['id'].'appcode.png');
        }
        return view('card/code',[
            'code'=> $filename,
            'name' =>  $cardRes->name
        ]);
        
    }

 
}
